import java.awt.*;

public class DefenseTower extends Unit {
    public DefenseTower(Team team, int x, int y, int attackRadius, int damage) {

        this.id = GameFrame.getRedTeamUnitID(team);
        this.team = team;
        this.x = x;
        this.y = y;
        this.attackRadius = attackRadius;
        this.damage = damage;
        this.hpLeft = 400;
        this.hpTotal = 400;
    }

    public void paint(Graphics graphics)
    {
        graphics.drawOval(x - attackRadius / 2, y - attackRadius / 2, attackRadius, attackRadius * 2 / 3);
        int x = this.x ;
        int y = this.y - 70;

        //画玩家血条
        graphics.setColor(new Color(255 - hpLeft * 255 / hpTotal , hpLeft * 255 / hpTotal, 0));
        graphics.fillRect(x, y, hpLeft * 100 / hpTotal, 10);
        y += 10;
        graphics.fillRect(x, y, 100, 10);

        y += 10;
        //画玩家蓝条
        graphics.setColor(new Color(0, 0, 255));
        graphics.drawRect(x, y, 40, 10);
        graphics.fillRect(x, y, 30, 10);

        graphics.setColor(new Color(0, 0,0 ));
        graphics.drawString("防御塔(" +x + "," + y + "), hpLeft: " + hpLeft,x,y);

        y += 30;
        //画玩家，现在是画一个小球
        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0, 0));
        }
        else
        {
            graphics.setColor(new Color(0, 0, 255));
        }

        graphics.fillRect(x, y, 30,60);
    }

    @Override
    void move() {

    }


    @Override
    void attack(Graphics graphics) {
        if (state != UnitState.Attack)
            return;

        final Unit self = attack.self;
        final Unit target = attack.target;
        int incrementX = target.x - self.x;
        int incrementY = target.y - self.y;
        int ix = incrementX / 20;
        int iy = incrementY / 20;

        //1、画子弹
        int x = self.x + timer1 * ix;
        int y = self.y + timer1 * iy;

        //画子弹
        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0,0));
        }
        else if (team == Team.BLUE)
        {
            graphics.setColor(new Color(0,0, 255));
        }

        if (timer1 < 20)
        {
            graphics.fillRect(x, y, 5, 5);

            //2、对方减血量
            if (Math.abs(x - target.x) < 16 || Math.abs(y - target.y) < 16)
            {
                timer1 = 0;
                target.hpLeft -= (int) (damage + Math.random() * 2); //攻击力是5-10
            }
        }
        else
            return;
        //logger.info(self.x + " " + self.y + " " + target.x + " " + target.y + " x,y: " + x + " " + y + " timer: " + timer1);

        timer1++;


    }
}
